package com.itheima.d9_test02;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class demo01 {
    public static void main(String[] args) throws IOException {
        // 用来记录所有英雄的数据
        ArrayList<Hero> heroes = new ArrayList<>();

        File hero = new File("hero.txt");
        String s1 = FileUtils.readFileToString(hero, "GBK");
        System.out.println("======读出来的数据======");
        System.out.println(s1);     // 读出来的数据


        System.out.println("=====过滤之后的======");
        String s2 = s1.replaceAll("[,]{2,}", ",");
        System.out.println(s2);

        System.out.println("==================");
        //已经拿到了每一行英雄的数据
        String[] split = s2.split("\r\n");
        for (int i = 1; i < split.length; i++) {
//            System.out.println(split[i]);
            String[] sp2 = split[i].split(",");     // 获取单个英雄每个数据
            String name = sp2[0];
            int survive = Integer.parseInt(sp2[1]);
            int attack = Integer.parseInt(sp2[2]);

            int effect = Integer.parseInt(sp2[3]);
            heroes.add(new Hero(name,survive,attack,effect));
        }

        System.out.println("集合数据");
        for (Hero hero1 : heroes) {
            System.out.println(hero1);
        }

        heroes.sort(((o1, o2) -> Double.compare(o2.getScore(),o1.getScore())));

        System.out.println("================");
        System.out.println("排序后：");
        for (Hero hero1 : heroes) {
            System.out.println(hero1);
        }




        File xs = new File("新手.txt");

        FileUtils.writeStringToFile(xs,"姓名 生存 攻击 效果 新手适应分值" +System.lineSeparator(),"GBK",false);

        for (Hero hero1 : heroes) {
            String s5 = String.valueOf(hero1);
            FileUtils.writeStringToFile(xs, s5+System.lineSeparator(),"GBK",true);
        }
    }


}
